package com.project.controller;

import java.util.Date;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.project.bean.OrderBean;
import com.project.bean.PageBean;
import com.project.service.ILessonService;
import com.project.service.IOrderService;
import com.project.service.IStudentService;

@Controller
public class OrderController {
	
	@Autowired
	private IOrderService orderService;
	@Autowired
	private IStudentService studentService;
	@Autowired
	private ILessonService lessonService;
	
	@RequestMapping(value = "/addOrder")
	@ResponseBody
	public int addOrder(int oLId,int oUId2,HttpSession session) {
		int oUId1 = (int) session.getAttribute("UId");
		int num = 0;
		double lPrice = lessonService.findLessonById(oLId).getlPrice();
		double sMoney = studentService.findStudentByUserId(oUId1).getsMoney();
		if(lPrice<=sMoney) {
			OrderBean order = new OrderBean();
			order.setoLId(oLId);
			order.setoUId1(oUId1);
			order.setoUId2(oUId2);
			order.setoState(1);
			Date date = new Date();
			order.setoTime(date);
			order.setoComment(3);
			num = orderService.addOrder(order);
		}
		return num;
	}
	
	@RequestMapping(value = "/coach_order")
	public String coachOrderState1(int state,Model model,HttpSession session) {
		int id=(int) session.getAttribute("UId");
		PageBean orders =  orderService.findAllToPageByCoach(id, 1, 5, state);
		model.addAttribute("orders", orders);
		model.addAttribute("state", state);
		return "coach_order.html";
		
	}
	
	@RequestMapping(value = "/student_order")
	public String studentOrderState1(int state,Model model,HttpSession session) {
		int id=(int) session.getAttribute("UId");
		PageBean orders =  orderService.findAllToPageByStu(id, 1, 5, state);
		model.addAttribute("orders", orders);
		model.addAttribute("state", state);
		return "student_order.html";
		
	}
	
	
	@RequestMapping(value = "/changeOrder")
	public String changeOrder(int oId,int state,Model model) {
		OrderBean order = new OrderBean();
		order.setoId(oId);
		order.setoState(state);
		orderService.changeState(order);
		return "redirect:/coach_order?state="+state;
	}
	
	@RequestMapping(value = "/stuChangeOrder")
	public String stuChangeOrder(int oId,int state,Model model) {
		OrderBean order = new OrderBean();
		order.setoId(oId);
		order.setoState(state);
		orderService.changeState(order);
		return "redirect:/student_order?state="+state;
	}
	
	@RequestMapping(value = "/deleteOrder")
	public String deleteOrder(int oId,Model model) {
		orderService.refuseOrder(oId);
		return "redirect:/coach_order?state=1";
	}
	
	@RequestMapping(value = "/stuDeleteOrder")
	public String stuDeleteOrder(int oId,Model model) {
		orderService.refuseOrder(oId);
		return "redirect:/student_order?state=1";
	}
	
	@RequestMapping(value = "/payOrder")
	public String payOrder(int oId,Model model) {
		OrderBean order = new OrderBean();
		order.setoId(oId);
		order.setoState(5);
		orderService.payOrder(order);
		return "redirect:/coach_order?state=5";
	}
	
	@RequestMapping(value = "/abandonOrder")
	public String abandonOrder(int oId,Model model) {
		OrderBean order = new OrderBean();
		order.setoId(oId);
		order.setoState(4);
		orderService.changeState(order);
		return "redirect:/coach_order?state=2";
	}
	
	@RequestMapping(value = "/completeOrder")
	@ResponseBody
	public int completeOrder(int oId,int comment,Model model) {
		OrderBean order = orderService.findById(oId);
		order.setoState(6);
		order.setoComment(comment);
		int num = orderService.completeOrder(order);
		return num;
		
	}
}
